#include "ScriptLoader.hpp"

std::map<const int, const Item*> ScriptLoader::m_items = std::map<const int, const Item*>();
std::map<const int, const Monster*> ScriptLoader::m_monsters = std::map<const int, const Monster*>();
ScriptLoader* ScriptLoader::_singletonScriptLoader = NULL;

ScriptLoader::ScriptLoader(App* appPtr): m_app(appPtr)
{


}

ScriptLoader* ScriptLoader::GetScriptLoader(App *appPtr)
{
    if(_singletonScriptLoader == NULL)
        _singletonScriptLoader = new ScriptLoader(appPtr);

    return _singletonScriptLoader;
}

void ScriptLoader::LoadItems()
{
	std::list<std::string> valuesLines;

    std::cout << "Loading Items Scripts ..." << std::endl;

	FileParser::AllLines(SCRIPT_ITEM_LOADER, valuesLines);

    for(std::list<std::string>::iterator it = valuesLines.begin(); it != valuesLines.end(); it++)
	{
		Item* item = new Item(m_app);
		item->LoadScript(*it);

        m_items.insert(std::pair<const int, const Item*>(item->ID(), item));

	}

}

void ScriptLoader::LoadMonsters()
{
    std::list<std::string> valuesLines;

    std::cout << "Loading Monsters Scripts ..." <<  std::endl;

    FileParser::AllLines(SCRIPT_MONSTER_LOADER, valuesLines);

    for(std::list<std::string>::iterator it = valuesLines.begin(); it != valuesLines.end(); it++)
    {
        Monster* monster = new Monster(m_app);
        monster->LoadScript(*it);

        //m_items.insert(std::pair<const int, const Monster*>(monster->ID(), monster));
    }
}

Item* ScriptLoader::CopyItem(int id)
{
    std::map<const int, const Item*>::iterator iter;

    iter = m_items.find(id);

    if(iter == m_items.end())
        return NULL;
    else
        return new Item(*(iter->second));
}
